<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ArticleType extends Model
{
    use HasFactory;

    protected $table = 'sp_article_type';
    protected $primaryKey = 'id';
    public $timestamps = false;

    //status状态 1显示  2隐藏
    const ARTICLE_TYPE_STATUS_DISPLAY = 1;//显示
    const ARTICLE_TYPE_STATUS_HIDE = 2;//隐藏

    //is_delete是否删除  1是 2否
    const ARTICLE_TYPE_IS_DELETE = 1;//是
    const ARTICLE_TYPE_NOT_DELETE = 2;//否

    //获取一级分类
    protected function GetParentCategory($where)
    {
        $category = $this
            ->where($where)
            ->orderBy('sort', 'asc')
            ->get();
        $category = $category ? $category->toArray() : [];
        return $category;
    }

    //获取二级分类
    protected function GetSecondCategory($where)
    {
        $category = $this
            ->where($where)
            ->orderBy('sort', 'asc')
            ->get();
        $category = $category ? $category->toArray() : [];
        return $category;
    }

    //根据id获取一级分类
    protected function GetParentCategoryById($id)
    {
        $where = array(
            'id' => $id,
            'status' => 1, //状态 1显示  2隐藏
            'is_delete' => 2, //是否删除  1是 2否
            'parentid' => 0,
        );
        $data = $this
            ->where($where)
            ->first();
        $data = $data ? $data->toArray() : [];
        return $data;
    }

    //查询一条文章分类
    protected function GetOneArticleType($where)
    {
        $data = $this->select('id', 'parentid', 'type_name')
            ->where($where)
            ->first();
        $data = $data ? $data->toArray() : [];
        return $data;
    }

    //添加文章分类
    protected function addData($data)
    {
        return $data = $this->insert($data);
    }

    //根据id查询文章分类
    protected function GetCategoryById($id)
    {
        $where = array(
            'id' => $id,
        );
        $data = $this
            ->where($where)
            ->first();
        $data = $data ? $data->toArray() : [];
        return $data;
    }

    //根据id修改文章分类
    protected function editDataById($id, $data)
    {
        $where = array(
            'id' => $id,
        );
        return $data = $this->where($where)->update($data);
    }


}
